package top.ucat.boots.example.oauth2.controller;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ucat.boots.common.result.Result;
import top.ucat.boots.common.result.SystemResult;
import top.ucat.boots.starter.oauth2.client.beans.oauth.OauthToken;
import top.ucat.boots.starter.oauth2.client.beans.oauth.OauthTokenDto;
import top.ucat.boots.starter.oauth2.client.beans.oauth.login.QRLoginDto;
import top.ucat.boots.starter.oauth2.client.definition.UserCodeTypeDefinition;
import top.ucat.boots.starter.oauth2.server.factory.DefaultLoginFactory;
import top.ucat.boots.starter.oauth2.server.factory.ScanQRSsoFactory;
import top.ucat.boots.starter.oauth2.server.utils.SubjectUtils;

@RestController
@RequestMapping("test")
public class TestController {


    @GetMapping("default")
    public Result dLog() {

        DefaultLoginFactory loginFactory = DefaultLoginFactory.getInstance();

        loginFactory.registerUserCredentials("1", "1", UserCodeTypeDefinition.userCode, "1");

        OauthTokenDto dto = new OauthTokenDto();
        dto.setUserCode("1");
        dto.setSystemType("1");
        dto.setGrantType("1");
        dto.setClientSecret("123456");
        dto.setClientId("system");

        OauthToken o = SubjectUtils.defaultLogin(dto);

        return SystemResult.OK.getResult(o);
    }

    @GetMapping("qr")
    public Result qr() {

        ScanQRSsoFactory scanQRSsoFactory = SubjectUtils.getSubject().getScanQRSsoFactory();
        scanQRSsoFactory.registerUserCredentials("1", "1", UserCodeTypeDefinition.QR, "1");

        QRLoginDto dto = new QRLoginDto();
        dto.setSystemType("1");
        dto.setGrantType("1");
        dto.setClientSecret("123456");
        dto.setClientId("system");
        dto.setUserSign("1");

        OauthToken o = SubjectUtils.qrLogin(dto);

        return SystemResult.OK.getResult(o);
    }

    public static void main(String[] args) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("123456");

        System.out.println(encode);
    }
}
